package com.autumn.chat.http;

import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.autumn.chat.config.GlobalParam;
import com.autumn.chat.constant.BiLiBiLiConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ClassName: DoGPTApi
 * Description:
 * Author: Shen_Yue
 * Date: 2023/3/3 20:26
 * History:
 * <author>          <time>          <version>          <desc>
 * wangqiuyang    2023/3/3 20:26   @Version 1.0        描述
 */
public class DoGPTApi {
    private static final Logger log = LoggerFactory.getLogger(DoGPTApi.class);

    /*
        openai文档给出的例子：
        request format:
        openai.ChatCompletion.create(
          model="gpt-3.5-turbo",
          messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": "Who won the world series in 2020?"},
                {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
                {"role": "user", "content": "Where was it played?"}
            ]
        )

        response format:

        {
         'id': 'chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve',
         'object': 'chat.completion',
         'created': 1677649420,
         'model': 'gpt-3.5-turbo',
         'usage': {'prompt_tokens': 56, 'completion_tokens': 31, 'total_tokens': 87},
         'choices': [
           {
            'message': {
              'role': 'assistant',
              'content': 'The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers.'},
            'finish_reason': 'stop',
            'index': 0
           }
          ]
        }
     */

    /**
    * Description:
     * 参考 ： https://platform.openai.com/docs/api-reference/chat
    * @param
    * @return: void
    * @author: Shen_Yue
    * @Date: 23:47 2023/3/3
    */
    public static String getChatMessage(String content) {
        JSONObject body = new JSONObject();
        // 选用模型
        body.put("model" , "gpt-3.5-turbo");
        // 生成的最大tokens数
        body.put("max_tokens", GlobalParam.MAX_TOKENS);

        JSONArray messages = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.put("role","user");
        obj.put("content",content);
        messages.add(obj);
        // 聊天内容
        body.put("messages", messages);

        log.info("请求GPT , 参数为： -->" + body );

        // 请求
        HttpRequest result = HttpRequest.post(BiLiBiLiConstant.OPEN_AI_GPT_URL)
                .setHttpProxy(GlobalParam.PROXY_HOST, GlobalParam.PROXY_PORT)
                .header(Header.CONTENT_TYPE, ContentType.JSON.getValue())
                .header(Header.AUTHORIZATION, "Bearer " + GlobalParam.OPEN_AI_SECRET_KEY)
                .body(body.toString());

        // 返回值
        String response = result.execute().body();

        log.info("请求GPT , 返回为： -->" + response );
        return response;
    }
}
